package com.kotlin.usermodel.ui.activity

import android.os.Bundle
import com.kotlin.baselibrary.common.AppManager
import com.kotlin.baselibrary.ext.trimEditString
import com.kotlin.baselibrary.ui.activity.BaseMvpActivity
import com.kotlin.usermodel.R.layout.activity_register
import com.kotlin.usermodel.injection.component.DaggerUserComponent
import com.kotlin.usermodel.injection.module.UserModule
import com.kotlin.usermodel.presenter.RegisterPresenter
import com.kotlin.usermodel.presenter.view.RegisterView
import kotlinx.android.synthetic.main.activity_register.*
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.toast

class RegisterActivity : BaseMvpActivity<RegisterPresenter>(), RegisterView {
    private var pressTime: Long = 0L


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(activity_register)

        btnRegister.onClick {
            mPresenter.register(
                edtPhoneNumber.trimEditString(),
                edtVerifyCode.trimEditString(),
                edtPwd.trimEditString()
            )
        }

        btnVerify.onClick {
            mPresenter.register2(
                edtPhoneNumber.trimEditString(),
                edtVerifyCode.trimEditString(),
                edtPwd.trimEditString()
            )
        }
    }

    override fun injectComponent() {
        DaggerUserComponent.builder().activityComponent(activityComponent).userModule(UserModule())
            .build().inject(this)
        mPresenter.mView = this
    }

    override fun onRegisterResult(result: String) {
        toast(result)
    }

    override fun onBackPressed() {
        val currentTime = System.currentTimeMillis()
        if (currentTime - pressTime > 2000) {
            toast("再按一次退出应用程序")
            pressTime = currentTime
        } else {
            AppManager.instance.exitApp(this)
        }
    }
}